<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button>按钮1</button>
<button>按钮2</button>
<button>按钮2</button>
<script>
var btns = document.querySelectorAll("button");
/*for (var i = 0; i < btns.length; i++){
    btns[i].onclick = (function (i){
        return function (){
            console.log(i);
        }
    })(i)
}*/

/*for (var i = 0; i < btns.length; i++){
    btns[i].index = i;
    btns[i].onclick = function (){
        console.log(this.index);
    }
}*/

for (let i = 0; i < btns.length; i++){
    btns[i].onclick = function (){
        console.log(i);
    }
}

</script>
</body>
</html>